using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Friso.Entities;
using Friso.Business;
using FetCo.Data;
using FetCo.Framework;

public partial class Admin_ContentMailList : System.Web.UI.Page
{
    #region Viarible
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    protected string hidColSort
    {
        get
        {
            object o = ViewState["hidColSort"];
            if (o == null) return "[" + Photo._Table + "].[" + PhotoColumns.CreatedDate + "]";
            return o.ToString();
        }
        set
        {
            ViewState["hidColSort"] = value;
        }
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["CurrentMenu"] = (int)Menus.Admin;
        Session["SubCurrentMenu"] = (int)SubMenus.ClipList;
        if (IsPostBack)
            return;
        LoadData();
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
            hyperlinkPager.CurrentIndex = currnetPageIndx;
            LoadData();
        }
        catch { Response.Redirect(RTISettings.ErrorLink); }
    }
    private void LoadData()
    {
        Criteria cri = new Criteria();

        int total = 0;
        hyperlinkPager.PageSize = RTISettings.PageSize;
        NoCountStart = (hyperlinkPager.PageSize * (hyperlinkPager.CurrentIndex - 1)) + 1;
        rpt_BannerList.DataSource = PhotoService.GetPhotoListByParam(this.hidColSort, (OrderDirection)Session["Order"], hyperlinkPager.CurrentIndex, hyperlinkPager.PageSize, cri.Criter, out total);
        rpt_BannerList.DataBind();
        hyperlinkPager.ItemCount = total;
        if (total / hyperlinkPager.PageSize == 0)
            hyperlinkPager.Visible = false;
    }
    protected void rpt_BannerList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Photo _Photo = (Photo)e.Item.DataItem;
                Literal litNo = (Literal)e.Item.FindControl("litNo");
                litNo.Text = Convert.ToString(NoCountStart + e.Item.ItemIndex);
                Literal ltr_CreateDate = (Literal)e.Item.FindControl("ltr_CreateDate");

                TextBox txt_Order = (TextBox)e.Item.FindControl("txt_Order");


                if (Utility.GetFormattedDate(_Photo.CreatedDate) != Name.DateDefault)
                    ltr_CreateDate.Text = Utility.GetFormattedDate(_Photo.CreatedDate);
                txt_Order.Text = _Photo.Order.ToString();
                HtmlInputHidden hidBanner = (HtmlInputHidden)e.Item.FindControl("hidBanner");
                hidBanner.Value = _Photo.PhotoId.ToString();


            }
        }
        catch { Response.Redirect(RTISettings.ErrorLink); }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (RepeaterItem item in this.rpt_BannerList.Items)
            {
                CheckBox chkBanner = (CheckBox)item.FindControl("chkBanner");
                if (chkBanner.Checked == true)
                {
                    HtmlInputHidden hidBanner = (HtmlInputHidden)item.FindControl("hidBanner");
                    string strDelete = hidBanner.Value.ToString();
                    long bannerId = long.Parse(strDelete);
                    try
                    {

                        PhotoService.DeletePhoto(bannerId);
                    }
                    catch
                    {
                        lb_error.Text = "Dont's delete this item because it is getting using another items";
                    }
                }
            }
            LoadData();
        }
        catch { Response.Redirect(RTISettings.ErrorLink); }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (RepeaterItem item in this.rpt_BannerList.Items)
            {
                HtmlInputHidden hidBanner = (HtmlInputHidden)item.FindControl("hidBanner");
                TextBox txt_Order = (TextBox)item.FindControl("txt_Order");
                CheckBox chkBanner = (CheckBox)item.FindControl("chkBanner");

                string strNewsletterId = hidBanner.Value.ToString();
                int newsletterId = int.Parse(strNewsletterId);
                Photo _Photo = PhotoService.GetPhoto(newsletterId);
                if (_Photo != null)
                {
                    if (txt_Order.Text.Trim() != string.Empty)
                        _Photo.Order = int.Parse(txt_Order.Text.Trim());
                    PhotoService.UpdatePhoto(_Photo);
                }
            }
            LoadData();
        }
        catch (Exception ex) { Response.Redirect(RTISettings.ErrorLink); }

    }
}
